

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/touxiang.jpg">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  
    <meta name="description" content="">
  
  <meta name="author" content="lc">
  <meta name="keywords" content="">
  
  <title>redis的学习 - Long</title>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.4.0/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"lcbc6.github.io","root":"/","version":"1.8.8","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>LC</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/t.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="redis的学习">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-05-07 20:08" pubdate>
        2021年5月7日 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.1k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      27
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">redis的学习</h1>
            
            <div class="markdown-body">
              <h1 id="redis的学习"><a href="#redis的学习" class="headerlink" title="redis的学习"></a>redis的学习</h1><h3 id="1-NoSQL数据库"><a href="#1-NoSQL数据库" class="headerlink" title="1.NoSQL数据库"></a>1.NoSQL数据库</h3><h4 id="1-1-NoSQL的概数"><a href="#1-1-NoSQL的概数" class="headerlink" title="1.1 NoSQL的概数"></a>1.1 NoSQL的概数</h4><p>​    非关系型数据库，不依赖业务逻辑方式存储，而已简单的key-value模式存储。增加了数据库的扩展能力。</p>
<ul>
<li>不遵循SQL标准</li>
<li>不支持ACID（一致性、隔离性、持久性、原子性）</li>
<li>远超于SQL性能</li>
</ul>
<h4 id="1-2-NoSQL适用场景"><a href="#1-2-NoSQL适用场景" class="headerlink" title="1.2 NoSQL适用场景"></a>1.2 NoSQL适用场景</h4><ul>
<li>对数据高并发的读写</li>
<li>数量数据的读写</li>
<li>对数据高扩展性的</li>
</ul>
<h4 id="1-3-NoSQL不适用场景"><a href="#1-3-NoSQL不适用场景" class="headerlink" title="1.3 NoSQL不适用场景"></a>1.3 NoSQL不适用场景</h4><ul>
<li>需要事务支持</li>
<li>基于sql的结构化查询，处理复杂的关系</li>
</ul>
<h3 id="2-Redis"><a href="#2-Redis" class="headerlink" title="2.Redis"></a>2.Redis</h3><h4 id="2-1Redis的安装"><a href="#2-1Redis的安装" class="headerlink" title="2.1Redis的安装"></a>2.1Redis的安装</h4><p>​     首先在<a target="_blank" rel="noopener" href="https://redis.io/">redis</a>官网下载redis然后上传到linux，通过<code>tar -zxvf redis.gz</code>将下载文件解压，然后进入解压文件夹输入<code>make</code>等待全部编译成功后在使用命令<code>make install </code>安装成功后进入<code>usr/local/bin</code>就可以运行redis了。</p>
<p>以下是将redis设置为后台运行</p>
<figure class="highlight awk"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs awk">cp redis.conf <span class="hljs-regexp">/etc/</span>redis.conf<br>vi <span class="hljs-regexp">/etc/</span>redis.conf<br>修改redis为后台启动<br>daemonize yes<br>cd <span class="hljs-regexp">/usr/</span>local/bin<br>redis-server <span class="hljs-regexp">/etc/</span>redis.conf <br></code></pre></td></tr></table></figure>

<h4 id="2-2-Redis相关知识"><a href="#2-2-Redis相关知识" class="headerlink" title="2.2 Redis相关知识"></a>2.2 Redis相关知识</h4><p>默认使用0号库，共有16个。</p>
<p>Redis是单线程+多路IO复用技术</p>
<p>串行 vs 多线程 + 锁（memcached） vs 单线程 + 多路IO复用（Redis）</p>
<p>串行：就像吃羊肉串只能一次一个的吃（按顺序）</p>
<p>单线程+多路IO复用</p>
<p>买票例子：</p>
<p><img src="C:\Users\ASUS\AppData\Roaming\Typora\typora-user-images\image-20210423215358609.png" srcset="/img/loading.gif" alt="image-20210423215358609"></p>
<h3 id="3-常用的五大数据类型"><a href="#3-常用的五大数据类型" class="headerlink" title="3.常用的五大数据类型"></a>3.常用的五大数据类型</h3><h4 id="3-1Redis键（key）"><a href="#3-1Redis键（key）" class="headerlink" title="3.1Redis键（key）"></a>3.1Redis键（key）</h4><figure class="highlight q"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs q"><span class="hljs-built_in">keys</span> * 查看当前库所有<span class="hljs-built_in">key</span>（匹配：<span class="hljs-built_in">keys</span> *<span class="hljs-number">1</span>）<br>exists <span class="hljs-built_in">key</span> 判断某个<span class="hljs-built_in">key</span>是否存在<br><span class="hljs-built_in">type</span> <span class="hljs-built_in">key</span> 查看<span class="hljs-built_in">key</span>的类型<br>del <span class="hljs-built_in">key</span> 删除指定的<span class="hljs-built_in">key</span>数据<br>unlink <span class="hljs-built_in">key</span> 根据<span class="hljs-built_in">value</span>选择非阻塞删除<br>expire <span class="hljs-built_in">key</span> <span class="hljs-number">10</span> 设置给定<span class="hljs-built_in">key</span>的过期时间<span class="hljs-number">10</span>s<br>ttl <span class="hljs-built_in">key</span> 查看还有多少秒过期，<span class="hljs-number">-1</span>表示永不过期，<span class="hljs-number">-2</span>表示已过期<br><span class="hljs-keyword">select</span> 命令切换数据库<br>dbsize 查看当前数据库的<span class="hljs-built_in">key</span>的数量<br>flushdb 清空当前库<br>flushall 通杀全部库<br></code></pre></td></tr></table></figure>

<h4 id="3-2Redis字符串（String）"><a href="#3-2Redis字符串（String）" class="headerlink" title="3.2Redis字符串（String）"></a>3.2Redis字符串（String）</h4><p>String类型是二进制安全的。意味着Redis的String可以包含任何数据。比如jpg图片或者序列化对象。</p>
<p>一个Redis中字符串value最多可以是512M</p>
<h5 id="3-2-1常用命令"><a href="#3-2-1常用命令" class="headerlink" title="3.2.1常用命令"></a>3.2.1常用命令</h5><p><code>set &lt;key&gt; &lt;value&gt;</code>添加键值对（如果本来就存在这个key就会覆盖当前key的value）</p>
<figure class="highlight bnf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs bnf"> get <span class="hljs-attribute">&lt;key&gt;</span> 查询对应键值<br> append <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;value&gt;</span> 将给定值追加到原值末尾<br> strlen <span class="hljs-attribute">&lt;key&gt;</span> 获取值长度<br> setnx <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;value&gt;</span> 只有在key不存在时 设置key值<br><br> incr <span class="hljs-attribute">&lt;key&gt;</span><br>将key中存储的数字值增1<br>只能对数字值操作，如果为空，新增值为1<br> decr <span class="hljs-attribute">&lt;key&gt;</span><br>将key中存储的值减1<br>只能对数字值操作，如果为空，新增值为-1<br> incrby/decrby <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;步长&gt;</span> 将key中存储的数字值增减。自定义步长<br> mset <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;value&gt;</span> <span class="hljs-attribute">&lt;key1&gt;</span> <span class="hljs-attribute">&lt;value1&gt;</span> ... 同时设置多个key-value<br> mget <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;key1&gt;</span> ... 同时获取多个key<br> msetnx <span class="hljs-attribute">&lt;key&gt;</span> <span class="hljs-attribute">&lt;value&gt;</span> <span class="hljs-attribute">&lt;key1&gt;</span> <span class="hljs-attribute">&lt;value1&gt;</span> ... 同时设置多个key-value，当且仅当所以给定key值不存在时<br> getrange <span class="hljs-attribute">&lt;key&gt;</span><span class="hljs-attribute">&lt;起始位置&gt;</span><span class="hljs-attribute">&lt;结束位置&gt;</span> 获取值的范围<br> setrange <span class="hljs-attribute">&lt;key&gt;</span><span class="hljs-attribute">&lt;起始位置&gt;</span><span class="hljs-attribute">&lt;value&gt;</span> 用<span class="hljs-attribute">&lt;value&gt;</span>覆盖<span class="hljs-attribute">&lt;key&gt;</span>所储存的字符串<br> setex <span class="hljs-attribute">&lt;key&gt;</span><span class="hljs-attribute">&lt;过期时间&gt;</span><span class="hljs-attribute">&lt;value&gt;</span> 设置键值的同时，设置过期时间，单位秒。<br> getset <span class="hljs-attribute">&lt;key&gt;</span><span class="hljs-attribute">&lt;value&gt;</span> 以新换旧，设置新值同时获取旧值<br></code></pre></td></tr></table></figure>

<h4 id="3-3Redis列表（List）"><a href="#3-3Redis列表（List）" class="headerlink" title="3.3Redis列表（List）"></a>3.3Redis列表（List）</h4><h5 id="3-3-1-简介"><a href="#3-3-1-简介" class="headerlink" title="3.3.1 简介"></a>3.3.1 简介</h5><p>单键多值</p>
<p>Redis列表是简单的字符串列表，按照插入顺序排序。可以添加一个元素到列表的头部或者尾部。</p>
<p>它的底层实际是个双向链表，对两端的操作性能很高，通过索引下标的操作中间的节点性能比较差。</p>
<p><img src="C:\Users\ASUS\AppData\Roaming\Typora\typora-user-images\image-20210424191114609.png" srcset="/img/loading.gif" alt="image-20210424191114609"></p>
<h5 id="3-3-2-常用命令"><a href="#3-3-2-常用命令" class="headerlink" title="3.3.2 常用命令"></a>3.3.2 常用命令</h5><figure class="highlight tcl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs tcl">lpush/rpush &lt;key&gt;&lt;value1&gt;&lt;value2&gt;&lt;value3&gt; ...从左边、右边插入一个或多个值。<br>lpop/rpop &lt;key&gt;从左边/右边吐出一个值。 值在键在，值光键亡。<br>rpoplpush &lt;key1&gt;&lt;key2&gt;从&lt;key1&gt;列表右边吐出一个值，插到&lt;key2&gt;<br><span class="hljs-keyword">lrange</span> &lt;key&gt;&lt;start&gt;&lt;stop&gt; 按照索引下标获得元素（从左到右）<br><span class="hljs-keyword">lrange</span> mylist <span class="hljs-number">0</span> <span class="hljs-number">-1</span> <span class="hljs-number">0</span>左边第一个，<span class="hljs-number">-1</span>右边第一个（<span class="hljs-number">0</span> <span class="hljs-number">-1</span>表示所有）<br><span class="hljs-keyword">lindex</span> &lt;key&gt;&lt;index&gt; 按照索引下标获得元素（从左到右）<br>llen&lt;key&gt; 获得列表长度<br><span class="hljs-keyword">linsert</span>&lt;key&gt; before&lt;value&gt;&lt;newvalue&gt;在&lt;value&gt;的后面插入&lt;newvalue&gt;的值<br>lrem&lt;key&gt;&lt;n&gt;&lt;value&gt;从左边删除n个value（从左到右）<br><span class="hljs-keyword">lset</span>&lt;key&gt;&lt;index&gt;&lt;value&gt;将列表key下标为index的值替换成value<br></code></pre></td></tr></table></figure>

<h4 id="3-4-Redis集合（set）"><a href="#3-4-Redis集合（set）" class="headerlink" title="3.4 Redis集合（set）"></a>3.4 Redis集合（set）</h4><h5 id="3-4-1-简介"><a href="#3-4-1-简介" class="headerlink" title="3.4.1 简介"></a>3.4.1 简介</h5><p>Set是String类型的无序集合。它的底层是value为null的hash表，所以添加、删除、查找的<code>复杂度都是O(1)</code></p>
<h5 id="3-4-2-常用命令"><a href="#3-4-2-常用命令" class="headerlink" title="3.4.2 常用命令"></a>3.4.2 常用命令</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs xml">sadd <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value2</span>&gt;</span> 将一个或多个member元素加入到集合key中，已经存在的member元素被忽略。<br>smembers <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> 取出集合中的所有值<br>sismember <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>判断集合<span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span>是否含有该<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>值，有1，没有为0<br>scard <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> 返回该集合的元素个数<br>srem <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value2</span>&gt;</span> ... 删除集合中的某个元素<br>spop <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> 随机从该集合中吐出一个值<br>srandmember <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">n</span>&gt;</span>随机从该集合中取出n个值。不会从集合中删除<br>smove <span class="hljs-tag">&lt;<span class="hljs-name">source</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">destination</span>&gt;</span>value 把集合中一个值从一个集合移动到另一个集合<br>sinter <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">key2</span>&gt;</span> 返回两个集合的交集元素<br>sunion <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">key2</span>&gt;</span>返回两个集合的并集元素<br>sdiff <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">key2</span>&gt;</span> 返回两个集合的差集元素（key1中的，不包括key2中的）<br></code></pre></td></tr></table></figure>

<h4 id="3-5-Redis哈希（Hash）"><a href="#3-5-Redis哈希（Hash）" class="headerlink" title="3.5 Redis哈希（Hash）"></a>3.5 Redis哈希（Hash）</h4><h5 id="3-5-1-简介"><a href="#3-5-1-简介" class="headerlink" title="3.5.1 简介"></a>3.5.1 简介</h5><p>是一个键值对集合</p>
<p>是一个String类型的field和value的映射表，hash特别适用于存储对象</p>
<p>类似于java中的Map&lt;String,Object&gt;</p>
<h5 id="3-5-2-常用命令"><a href="#3-5-2-常用命令" class="headerlink" title="3.5.2 常用命令"></a>3.5.2 常用命令</h5><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs xml">hset <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span> 给<span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span>集合中的<span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span>键赋值<span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span><br>hget <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span> 从<span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span>集合<span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span>中取出value<br>hmest <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId2</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value2</span>&gt;</span>...批量设置hash的值<br>hexists<span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span>查看哈希表key中，给定域fieId是否存在<br>hkeys <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> 列出该hash集合的所有fieId<br>hvals <span class="hljs-tag">&lt;<span class="hljs-name">key</span>&gt;</span> 列出该hash集合的所有value<br>hincrby <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">increment</span>&gt;</span>为哈希表key中的域fieId的值加上增量1 -1<br>hsetnx <span class="hljs-tag">&lt;<span class="hljs-name">key1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">fieId</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">value</span>&gt;</span>将哈希表key中的域fieId的值设置为value，当且仅当fieid不存在<br></code></pre></td></tr></table></figure>

<h4 id="3-6-Redis有序集合（Zset）"><a href="#3-6-Redis有序集合（Zset）" class="headerlink" title="3.6 Redis有序集合（Zset）"></a>3.6 Redis有序集合（Zset）</h4><h5 id="3-6-1-简介"><a href="#3-6-1-简介" class="headerlink" title="3.6.1 简介"></a>3.6.1 简介</h5><p>zset与普通集合set非常相似，是一个没有重复元素的字符串集合。</p>
<p>不同之处是有序集合的每个成员都关联了一个评分（score），这个评分（score）被用来按照从低到高的方式排序集合中的成员。集合的成员是唯一的，但是评分是可以重复的。</p>
<h5 id="3-6-2-常用命令"><a href="#3-6-2-常用命令" class="headerlink" title="3.6.2 常用命令"></a>3.6.2 常用命令</h5><figure class="highlight pf"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs pf">zadd <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;score1&gt;</span><span class="hljs-variable">&lt;value1&gt;</span><span class="hljs-variable">&lt;score2&gt;</span><span class="hljs-variable">&lt;value2&gt;</span>... 将一个或多个member元素及其score值加入到有序集key当中<br>zrange <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;start&gt;</span><span class="hljs-variable">&lt;stop&gt;</span> [WITHSCORES] 返回有序集key中，下标在<span class="hljs-variable">&lt;start&gt;</span><span class="hljs-variable">&lt;stop&gt;</span>之间的元素，带WITHSCORES可以让分数一起和值返回到结果集<br>zrangbyscore key minmax [withscores] [<span class="hljs-keyword">limit</span> offset count] 返回有序集key中，所有score值介于<span class="hljs-keyword">min</span>和<span class="hljs-keyword">max</span>之间（包括等于<span class="hljs-keyword">min</span>和<span class="hljs-keyword">max</span>）的成员。有序集成员按score值递增（从小到大）次序排列<br>zrevrangbyscore key maxmin [withscores][<span class="hljs-keyword">limit</span> offset count] 同上，改为从大到小排序<br>zincrby <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;increment&gt;</span><span class="hljs-variable">&lt;value&gt;</span> 为元素的score加上增量<br>zrem <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;value&gt;</span> 删除该集合下，指定值的元素<br>zcount <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;min&gt;</span><span class="hljs-variable">&lt;max&gt;</span> 统计该集合，分数区间内的元素个数<br>zrank <span class="hljs-variable">&lt;key&gt;</span><span class="hljs-variable">&lt;value&gt;</span> 返回改值在集合中的排名，从<span class="hljs-number">0</span>开始<br></code></pre></td></tr></table></figure>

<h4 id="4-Redis的发布与订阅"><a href="#4-Redis的发布与订阅" class="headerlink" title="4.Redis的发布与订阅"></a>4.Redis的发布与订阅</h4><p>Redis发布订阅(pub/sub)是一种消息通信模式：发送者（pub）发送消息，订阅者（sub）接收消息</p>
<p>Redis客户端可以订阅任意数量的频道</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/redis/">redis</a>
                    
                  </div>
                
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/07/13/SpringSecurity/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">SpringSecurity的学习</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/04/27/vue/">
                        <span class="hidden-mobile">vue的学习</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <input type="hidden" name="测试是不是真的发布到了网站上">
  <iframe frameborder="yes" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1822538249&auto=1&height=66"></iframe>
  <div class="footer-content">
     <div> <span id="timeDate">载入天数...</span> <span id="times">载入时分秒...</span> <script src="/js/duration.js"></script> </div> <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a>

  </div>
  

  

  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      $('#local-search-input').on('click', function() {
        searchFunc(path, 'local-search-input', 'local-search-result');
      });
      $('#modalSearch').on('shown.bs.modal', function() {
        $('#local-search-input').focus();
      });
    })()
  </script>














  
<script src="/js/myjs.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
